package com.server;

import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;

/**
 * 拦截器
 */
public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

    //设置拦截位置
    public AuthInterceptor() {
        super(Phase.PRE_PROTOCOL);
    }

    //    处理拦截信息
    @Override
    public void handleMessage(SoapMessage soapMessage) {

        //登录检查参数
        for (Header header : soapMessage.getHeaders()) {
            SoapHeader soapHeader = (SoapHeader) header;
            Element e = (Element) soapHeader.getObject();
            String mAccount = e.getElementsByTagName("account").item(0).getTextContent();
            String mPassword = e.getElementsByTagName("password").item(0).getTextContent();

            if (StringUtils.isEmpty(mAccount) || StringUtils.isEmpty(mPassword) || !("root".equals(mAccount) && "123456".equals(mPassword))) {
                throw new Fault(new IllegalArgumentException("账号或密码错误！  root  123456"));
            }
        }

    }

}